package com.cosina.game.crystallight.engine;

import com.cosina.game.crystallight.MediaPlayerAdapter;
import com.cosina.game.crystallight.engine.util.Time;
import com.cosina.game.crystallight.engine.util.UpCount;
import com.cosina.game.crystallight.model.battle.BattleFieldProxy;

public class EvilNest implements Time{
	
	UpCount refreshCount = new UpCount(20);
	
	private final EnemyManager enemyManager;
	private final int waveCount;
	public EvilNest(EnemyManager enemyManager,BattleFieldProxy tollGate){
		this.enemyManager = enemyManager;
		this.waveCount = tollGate.getWaveCount();
	}

	public int currentWave = 0;
	
	public boolean hasNextLevel(){
		return currentWave < waveCount;
	}
	
	public float score = 310f;
	public int index = 10;
	public int maxIndex = 66;
	public float speed = score/56/refreshCount.max;
	
	//294 84 
	@Override
	public void withTimeGo() {
		if(null != newWaveFrameIndex){
			if(newWaveFrameIndex.upToAlram()){
				newWaveFrameIndex = null;
			}
		}
		
		if(hasNextLevel() == false)
			return ;
		
		if(refreshCount.upToAlram()){
			BattleEngine.manaFlask.up();
			if(hasNextLevel() == false){
				return ;
			}else{
				index ++;
			}
		}
		if(knocking == false)
			score -= speed;
		
		if(isTime2Go()){
			knocking = false;
			
			MediaPlayerAdapter.waveNext();
			enemyManager.newWaveGo(currentWave);
			
			newWaveFrameIndex = new UpCount(16);
			BattleEngine.speedManager.swtich(SpeedManager.SPEED_COMMON);
			currentWave++;
			score = 210  + currentWave * 84;
			speed = score/38/20;
			
			index = 28;
			refreshCount = new UpCount(20);
		}
	}
	
	public boolean isTime2Go(){
		if(knocking)
			return index == 66;
		else 
			return score < 0;
	}
	
	public UpCount newWaveFrameIndex;
	
	public boolean knocking = false;
	
	public void knockDoor() {
		if(knocking == true)
			return ;
		
		if(hasNextLevel()){
			knocking = true;
			int intScore = Integer.valueOf(String.valueOf(score).split("\\.")[0]);
			BattleEngine.flyingManager.flyingScore(intScore);
			{
				refreshCount = new UpCount(1);
			}
			MediaPlayerAdapter.waveForce();
		}
	}
}
